//
// Copyright (c) 2004 BroadVision, Inc. All rights reserved.
//
// This software is copyrighted.  Under the copyright laws, this software
// may not be copied, in whole or in part, without prior written consent
// of BroadVision, Inc. or its assignees. This software is provided under
// the terms of a license between BroadVision and the recipient, and its
// use is subject to the terms of that license.
//
// This software may be protected by one or more U.S. and International
// patents. Certain applications of BroadVision One-To-One software are
// covered by U.S. patent 5,710,887.
//
// TRADEMARKS: BroadVision and the BroadVision logo are registered
// trademarks, and BroadVision One-To-One is a trademark of BroadVision,
// Inc. IONA and Orbix are trademarks of IONA Technologies, Ltd. RSA,
// MD5, and RC2 are trademarks of RSA Data Security, Inc. All other
// trademarks, service marks, and trade names belong to their respective
// owners. BroadVision, Inc. disclaims any proprietary interest in the
// marks and names of others.
//

//
//
//
function Calendar(name, anchor, m, d, y, form, field) {
  var today = new Date;

  if (m == null) m = today.getMonth();
  if (d == null) d = today.getDate();
  if (y == null) y = today.getFullYear();

  this.m = m;
  this.d = d;
  this.y = y;

  this.form = form;
  this.field = field;

  this.id = g_calendars.length;
  g_calendars[this.id] = this;

  this.popup = new Popup(name, anchor);

  var dateLocale = new DateLocale();

  this.monthNames = dateLocale.getMonthNames();
  this.weekdayNames = dateLocale.getWeekdayNames();
  this.dateOrder = dateLocale.getDateOrder();
  this.prevImg = dateLocale.getPrevImg();
  this.nextImg = dateLocale.getNextImg();

  // Define the "methods"
  this.show = _calendar__show;
  this.setDate = _calendar__setDate;
  this.refresh = _calendar__refresh;
  this.toString = _calendar__toString;
  this.select = _calendar__select;
  this.monthAsOptions = _calendar__monthAsOptions;
  this.yearAsOptions = _calendar__yearAsOptions;
  this.header = _calendar__header;
  this.prev = _calendar__prev;
  this.next = _calendar__next;
  this.subheader = _calendar__subheader;
  this.body = _calendar__body;
  this.footer = _calendar__footer;

  this.popup.setContents(this.toString(this.m, this.d, this.y));
  this.popup.refresh();
}

//
//
//
function _calendar__show(bVisible) {
  this.popup.show(bVisible);
}

//
//
//
function _calendar__setDate(m, d, y) {
  this.m = m;
  this.d = d;
  this.y = y;
}

//
//
//
function _calendar__refresh() {
  this.popup.setContents(this.toString(this.m, this.d, this.y));
  this.popup.refresh();
}

//
//
//
function _calendar__select(m, d, y) {
  this.show(false);
  setDate(y, m, d, this.form, this.field);
}

//
// Handle the event that a year or month has been selected from the dropdowns.
//
function c_onChange(id, m, d, y) {
  var calendar = g_calendars[id];
  calendar.setDate(m, d, y);
  calendar.refresh();
}

//
// The user has selected a date. Pass the date values back to
// the parent window's callback function 'setDate()'. And then
// close the popup window.
//
function c_setDate(id, y, m, d) {
  var calendar = g_calendars[id];
  calendar.select(m, d, y);
}

//
// Handle the event that the next or previous month arrow has been clicked.
//
function c_onMonth(id, month, day, year, dir) {
  var m = month + dir;
  var y = year;

  if (m < 0) {
    m = 11;
    y-- ;
  }
  else if (m > 11) {
    m = 0;
    y++ ;
  }

  c_onChange(id, m, day, y);
}

//
// Returns the number of days for the given month and year.
//
function getDaysInMonth(year, month) {
  var DaysInMonth = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
  var mi = month ;
  var result = DaysInMonth[mi];

  if (mi == 1) {
    if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
      result = 29;
    }
  }

  return result;
}

//
// Render a month view.
//
function _calendar__toString(m, d, y) {
  var today = new Date;

  if (m == null) m = today.getMonth();
  if (d == null) d = today.getDate();
  if (y == null) y = today.getFullYear();

  var month = m;
  var year = y;

  var dt = new Date(year, month, 1);
  var weekDay = dt.getDay() ;

  var offset = (weekDay == 7) ? 0 : weekDay;
  var days = getDaysInMonth(year, month);
  var sum = days + offset;
  var rows = (sum / 7);

  var cursor = 0;
  var day = 1;

  var s;

  s  = '<form name=monthView>';
  s += '<table cellpadding=0 cellspacing=1 border=0 width=144 height=120 bgcolor=#dcdcdc>';

  s += this.header();
  s += this.subheader();
  s += this.body();
  s += this.footer();

  s += '</table>';
  s += '</form>';

  return s;
}

//
//
//
function _calendar__monthAsOptions() {
  var onchange = 'c_onChange(' + this.id + ', this[selectedIndex].value, ' + this.d + ', ' + this.y + ')';
  var s = '<select name="startMonth" onChange="' + onchange + '" class="cText">';
  for (var i = 0; i < this.monthNames.length; i++) {
    s += '<option value=' + i + (this.m == i ? ' selected' : '') + '>' + this.monthNames[i] + '</option>';
  }
  s += '</select>';

  return s;
}

//
//
//
function _calendar__yearAsOptions() {
  var today = new Date;
  var onchange = 'c_onChange(' + this.id + ', ' + this.m + ', ' + this.d + ', this[selectedIndex].value)';
  var s = '<select name="startYear" onChange="' + onchange + '" class="cText">';
  var found = false;
  for (var i = today.getFullYear() - 10 ; i < today.getFullYear() + 10; i++) {
    s += '<option value=' + i + (this.y == i ? ' selected' : '') + '>' + i + '</option>' ;
    if (this.y == i) found = true;
  }
  if (!found) {
    s += '<option value=' + this.y + ' selected>' + this.y + '</option>' ;
  }
  s += '</select>';

  return s;
}

//
//
//
function _calendar__header() {
  var s;

  s  = '<tr class="cHeader">'
  s +=   '<td>';
  s +=   this.prev();
  s +=   '</td>';
  s +=   '<td colspan=5>';

  if (this.dateOrder == "YMD" || this.dateOrder == "YDM") {
    s += this.y + '&nbsp;' + this.monthNames[this.m];
  }
  else {
    s += this.monthNames[this.m] + '&nbsp;' + this.y;
  }
  s +=   '</td>';
  s +=   '<td>';
  s +=   this.next();
  s +=   '</td>';
  s += '</tr>';

  return s;
}

//
//
//
function _calendar__prev() {
  var onmonth = 'c_onMonth(' + this.id + ', ' + this.m + ', ' + this.d + ', ' + this.y + ', -1)';
  var s = '<a href="#" onClick="' + onmonth + '">';
  s += this.prevImg;
  s += '</a>';

  return s;
}

//
//
//
function _calendar__next() {
  var onmonth = 'c_onMonth(' + this.id + ', ' + this.m + ', ' + this.d + ', ' + this.y + ', 1)';
  var s = '<a href="#" onClick="' + onmonth + '">';
  s += this.nextImg;
  s += '</a>';

  return s;
}

//
//
//
function _calendar__subheader() {
  var s = '<tr class="cSubheader">';
  s += '<td>' + this.weekdayNames[0] + '</td>';
  s += '<td>' + this.weekdayNames[1] + '</td>';
  s += '<td>' + this.weekdayNames[2] + '</td>';
  s += '<td>' + this.weekdayNames[3] + '</td>';
  s += '<td>' + this.weekdayNames[4] + '</td>';
  s += '<td>' + this.weekdayNames[5] + '</td>';
  s += '<td>' + this.weekdayNames[6] + '</td>';
  s += '</tr>';

  return s;
}

//
//
//
function _calendar__body() {
  var month = this.m;
  var year = this.y;

  var dt = new Date(year, month, 1);
  var weekDay = dt.getDay() ;

  var offset = (weekDay == 7) ? 0 : weekDay;
  var days = getDaysInMonth(year, month);
  var sum = days + offset;
  var rows = (sum / 7);

  var prevMonth = month - 1;
  var prevYear = year;
  if (month == 0) {
    prevMonth = 11;
    prevYear--;
  }
  var prevDays = getDaysInMonth(prevYear, prevMonth);
  var prevDay = prevDays - offset + 1;

  var nextMonth = month - 0 + 1;
  var nextYear = year;
  if (month == 11) {
    nextMonth = 0;
    nextYear++;
  }
  var nextDay = 1;


  var cursor = 0;
  var day = 1;

  var s = "";
  var styleClass = 'cBody';

  for (var r = 0; r < rows; r++) {
    s += '<tr>';

    for (c = 0; c < 7; c++) {
      if (cursor < offset) {
        s += '<td class="cOther">';
        s += '<a href="#" onClick="c_setDate(' + this.id + ', ' + prevYear + ',' + prevMonth + ',' + prevDay + ')" class="cOther">';
        s += prevDay;
        s += '</a>';
        s += '</td>';
        prevDay++;
      }
      else if (day > days) {
        s += '<td class="cOther">';
        s += '<a href="#" onClick="c_setDate(' + this.id + ', ' + nextYear + ',' + nextMonth + ',' + nextDay + ')" class="cOther">';
        s += nextDay;
        s += '</a>';
        s += '</td>';
        nextDay++;
      }
      else {
        s += '<td class="' + styleClass + '">';
        s += '<a href="#" onClick="c_setDate(' + this.id + ', ' + year + ',' + month + ',' + day + ')" class="' + styleClass + '">';
        s += day;
        s += '</a>';
        s += '</td>';
        day++;
      }

      cursor++;
    }

    s += '</tr>';
  }

  return s;
}

//
//
//
function _calendar__footer() {
  var s = '<tr>';
  s += '<td colspan=7>';

  if (this.dateOrder == "YMD" || this.dateOrder == "YDM") {
    s += this.yearAsOptions();
    s += this.monthAsOptions();
  }
  else {
    s += this.monthAsOptions();
    s += this.yearAsOptions();
  }

  s += '</td>';
  s += '</tr>';

  return s;
}


//
// Globals
//

  var g_calendars = new Array;
